package com.lhr.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.lhr.entity.Menu;
import com.lhr.mapper.MenuMapper;
import com.lhr.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhr.utils.MenuTreeBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 菜单表 服务实现类 树状菜单项
 *
 * @author LiHuiRu
 * @since 2023-02-21
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<Menu> findMenuTree() {
        List<Menu> menus = this.baseMapper.selectList(null);
        List<Menu> menuList = MenuTreeBuilder.build(menus);
        return menuList;
    }

    @Override
    public List<Long> findOpenIds() {
        List<Long> ids = new ArrayList<>();
        List<Menu> menus = this.baseMapper.selectList(null);
        if (!CollectionUtils.isEmpty(menus)) {
            for (Menu menu : menus) {
                if (menu.getOpen() == 1) {  //1表示打开的
                    ids.add(menu.getId());
                }
            }
        }
        return ids;
    }
}
